/**
 * This code is released under lgpl v3 license.
 * All rights reserved.
*/

package flash.wrapper.upload;

import bridge.FABridgeInstance;
import bridge.FABridgeWrapper;
import bridge.InitCallback;
import flash.display.DisplayObject;
import flash.display.DisplayObjectWrapper;
import flash.events.AS3EventHandler;
import flash.events.HasAS3EventHandler;
import flash.wrapper.FABridgeWidget;

/**
 * a button extends SwfWidget, and use fabridge system for upload file
 * @author yuan yunchang    dukerr@gmail.com
 */
public abstract  class UploadButton extends FABridgeWidget implements HasAS3EventHandler{
    public static final String DEFAULT_BUTTON_NAME = "default_upload_button.swf";
    
    /**
     * a wrapper for swf object.for add event handler
     */
    protected DisplayObjectWrapper nativeWrapper;
    /**
     * button name
     */
    String buttonName;


    public UploadButton(String src, String bridgeName,String width, String height) {
        super(src, width, height);
        setBridgeName(bridgeName);
        FABridgeWrapper.get().addInitCallback(bridgeName, new InitCallback() {
            public void initComplete(FABridgeInstance inst) {
                UploadRoot root =(UploadRoot)inst.root();
                DisplayObject btn = root.getTriggerButton();
                nativeWrapper = new DisplayObjectWrapper(UploadButton.this.bridgeName);
                nativeWrapper.setNativeAS3Obj(btn);
                buttonInitComplete();
            }
        });
    }
    
    /**
     * name of upload button
     * @return
     */
    public String getButtonName() {
        return buttonName;
    }

    public void setButtonName(String buttonName) {
        this.buttonName = buttonName;
        addFlashVar("buttonName", buttonName);
    }

   public void addHandler(AS3EventHandler handler, String eventType) {
        nativeWrapper.addHandler(handler, eventType);
    }
    
    
    /**
     * when init complete,this method is called, application
     * should use this method to add event handler to FileReferenceWrapper
     * from getFile
     */
    public abstract void buttonInitComplete();

    
    
}
